﻿Imports ControladorasAT.SIREOS
Imports Utilitarios
Imports ServiciosAT
Imports System.Data


Partial Class Pages_Default
    Inherits System.Web.UI.Page

    Private selID As Decimal

#Region "Metodos"

    Private Sub listarOrdenes()
        Dim oOrdenServicioBE As New SireosWSServicio.OrdenServicio
        Dim oEquipoBE As New SireosWSServicio.Equipo
        Dim oModeloBE As New SireosWSServicio.Modelo

        oOrdenServicioBE.categoriaServicio = Convert.ToInt32(ddlbCategoria.SelectedValue)
        oOrdenServicioBE.tipoServicio = Convert.ToInt32(ddlbtTipo.SelectedValue)
        oModeloBE.codMarca = Convert.ToInt32(ddlbMarca.SelectedValue)
        oEquipoBE.serEquipo = txbSerie.Text
        oEquipoBE.modelo = oModeloBE
        oOrdenServicioBE.equipo = oEquipoBE

        oOrdenServicioBE.codigoReferencia = txbOrden.Text
        oOrdenServicioBE.casoCliente = txbCaso.Text

        Dim oClienteBE As New SireosWSServicio.Cliente
        Dim oSedeBE As New SireosWSServicio.Sede
        oClienteBE.nomCliente = txbCliente.Text
        oClienteBE.nroDocumento = txbDocumento.Text
        oSedeBE.cliente = oClienteBE
        oOrdenServicioBE.sede = oSedeBE

        oOrdenServicioBE.modalidadServicio = Convert.ToInt32(ddlbModalidad.SelectedValue)

        Dim oCtServicio As New CtServicio
        Dim dt As DataTable = oCtServicio.listarOrdenesServicios(oOrdenServicioBE, txbInicio.Text, txbFin.Text)
        With gridOrdenes
            .DataSource = dt
            .DataBind()
        End With
        If dt Is Nothing Then
            LblTotal.Text = "Total: 0 Registros."
        Else
            LblTotal.Text = "Total: " + dt.Rows.Count.ToString + " Registros"
        End If
    End Sub

    Private Sub configuracionOrden(fila As GridViewRow)
        Dim ibAnular As ImageButton = DirectCast(fila.FindControl("imgAnular"), ImageButton)
        Dim ibAgendar As ImageButton = DirectCast(fila.FindControl("imgAgendar"), ImageButton)
        Dim ibEditar As ImageButton = DirectCast(fila.FindControl("imgEditar"), ImageButton)
        Dim esta As String = fila.Cells(7).Text
        Select Case esta
            Case "Orden Anulada"
                ibAnular.Visible = False
                ibAgendar.Visible = False
                ibEditar.Visible = False
            Case Else
                ibAnular.Visible = True
                ibAgendar.Visible = True
                ibEditar.Visible = True
        End Select
    End Sub

#End Region

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Listas.ListarTabla(Valores.Tabla.ModalidadServicio, ddlbModalidad)
            Listas.listarTabla(Valores.Tabla.CategoriaServicio, ddlbCategoria)
            Listas.listarTabla(Valores.Tabla.TipoServicio, ddlbtTipo)
            Listas.listarMarcasEquipo(ddlbMarca)
        End If
    End Sub

    Protected Sub ibtnBuscar_Click(sender As Object, e As ImageClickEventArgs) Handles ibtnBuscar.Click
        ListarOrdenes()
    End Sub

    Protected Sub ibtnNuevo_Click(sender As Object, e As ImageClickEventArgs) Handles ibtnNuevo.Click
        Response.Redirect("OrdenServicioRegistroOnSite.aspx?ModoPagina=" & Valores.ModoPagina.Nuevo)
    End Sub

    Protected Sub gridOrdenes_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gridOrdenes.PageIndexChanging
        ListarOrdenes()
        gridOrdenes.PageIndex = e.NewPageIndex
        gridOrdenes.DataBind()
    End Sub

    Protected Sub gridOrdenes_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gridOrdenes.RowDataBound
        Dim oImageButton As ImageButton = Nothing
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                oImageButton = DirectCast(e.Row.FindControl("imgDetalle"), ImageButton)
                If oImageButton IsNot Nothing Then
                    oImageButton.CommandArgument = Convert.ToString(e.Row.RowIndex)
                End If
                oImageButton = DirectCast(e.Row.FindControl("imgNotas"), ImageButton)
                If oImageButton IsNot Nothing Then
                    oImageButton.CommandArgument = Convert.ToString(e.Row.RowIndex)
                End If
                oImageButton = DirectCast(e.Row.FindControl("imgEditar"), ImageButton)
                If oImageButton IsNot Nothing Then
                    oImageButton.CommandArgument = Convert.ToString(e.Row.RowIndex)
                End If
                oImageButton = DirectCast(e.Row.FindControl("imgAgendar"), ImageButton)
                If oImageButton IsNot Nothing Then
                    oImageButton.CommandArgument = Convert.ToString(e.Row.RowIndex)
                End If
                oImageButton = DirectCast(e.Row.FindControl("imgAnular"), ImageButton)
                If oImageButton IsNot Nothing Then
                    oImageButton.CommandArgument = Convert.ToString(e.Row.RowIndex)
                End If
                Dim fila As GridViewRow = e.Row
                Me.configuracionOrden(fila)
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub gridOrdenes_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gridOrdenes.RowCommand
        Try
            selID = Convert.ToInt32(gridOrdenes.Rows(Convert.ToInt32(e.CommandArgument)).Cells(0).Text)
            If e.CommandName = "cnVerOrden" Then
                Response.Redirect("OrdenServicioRegistroOnSite.aspx?" + Valores.modo + Valores.igual + Valores.modoPagina.Consulta.ToString + Valores.amperson + Valores.OrdenServicio.CodOrden.ToString + Valores.igual + selID.ToString)
            ElseIf e.CommandName = "cnNotas" Then
                Session("CodigoOrden") = selID.ToString
                OrdenServicioNota1.limpiarCampos()
                popupNota.Show()
            ElseIf e.CommandName = "cnEditar" Then
                Response.Redirect("OrdenServicioRegistroOnSite.aspx?" + Valores.modo + Valores.igual + Valores.modoPagina.Editar.ToString + Valores.amperson + Valores.OrdenServicio.CodOrden.ToString + Valores.igual + selID.ToString)
            ElseIf e.CommandName = "cnAgendar" Then
                Response.Redirect("OrdenServicioAgenda.aspx")
            ElseIf e.CommandName = "cnAnular" Then
                LblOrden.Text = selID.ToString
                txbMotivo.Text = Valores.vacio
                popupAnularOrden.Show()
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub ibtnExportar_Click(sender As Object, e As ImageClickEventArgs) Handles ibtnExportar.Click

    End Sub

#Region "Popup"

    Protected Sub ibtnAnular_Click(sender As Object, e As ImageClickEventArgs) Handles ibtnAnular.Click
        If txbMotivo.Text = Valores.vacio Then
            Helper.mensajePanel(uppListarOrdenes, Mensajes.ingresarMotivoAnulacion)
        Else
            Dim oOrdenBE As New SireosWSServicio.OrdenServicio
            oOrdenBE.codOrden = LblOrden.Text
            oOrdenBE.codEstadoServicio = Valores.estadoServicio.OrdenAnulada
            oOrdenBE.usuEliminacion = Session("CodigoUsuario")
            oOrdenBE.motivoAnulacion = txbMotivo.Text

            Dim oCtServicio As New CtServicio
            oCtServicio.anularOrdenServicio(oOrdenBE)
            Helper.mensajePanel(uppListarOrdenes, Mensajes.correctoAnulado)
            txbMotivo.Text = Valores.vacio
            listarOrdenes()
            popupAnularOrden.Hide()
        End If
    End Sub

    Protected Sub ibtnCerrarPanel_Click(sender As Object, e As ImageClickEventArgs) Handles ibtnCerrarPanel.Click
        popupAnularOrden.Hide()
    End Sub

    Protected Sub ibtnCerrarNota_Click(sender As Object, e As ImageClickEventArgs) Handles ibtnCerrarNota.Click
        Session("CodigoOrden") = Valores.vacio
        popupNota.Hide()
    End Sub

#End Region
End Class

